/**
 * 大类控件
 * @author 吕少俊
 * 
 */
package com.zjht.gmcc.bean.tag;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;

public class SelectTypeNameTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	private String name;
	private String id;
	private String firstoption;
	private String firstvalue;
	private String onchange;
	private String selected;
	private boolean disabled;

	public SelectTypeNameTag() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		String name = this.name;
		String id = this.id;
		String firstoption = this.firstoption;
		String firstvalue = this.firstvalue;
		String onchange = this.onchange;
		String selected = this.selected;
		boolean disabled=this.disabled;
		
		String strSQL = "";
		String s_typeid = "";
		if (name == null || name.length() == 0) {
			name = "typeid";
		}
		if (id == null || id.length() == 0) {
			id = name;
		}
		
		StringBuilder html = new StringBuilder();
		html.append("<select name=\"");
		html.append(name);
		html.append("\"	id=\"");
		html.append(id);
		html.append("\" ");
		if (onchange != null && onchange.length() > 0) {
			html.append("onchange=\"");
			html.append(onchange);
			html.append("\"");
		}
		if(disabled){
			html.append(" disabled ");
		}
		html.append(">");
		if (!MyConstant.SELECT_FIRST_OPTION_NO.equals(firstoption))// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
		{
			if (MyFormat.isStrNull(firstoption))
				firstoption = "全部";

			if (MyFormat.isStrNull(firstvalue))
				firstvalue = MyConstant.SELECT_FIRST_VALUE;

			html.append("<option value=\"");
			html.append(firstvalue);
			html.append("\">");
			html.append(firstoption);
			html.append("</option>");
		}
		
			
			MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			strSQL = "SELECT typeid,typename FROM producttype WHERE levelid='1'";
			PreparedStatement ps = myJDBC.prepareStatement(strSQL);	
			ResultSet rs = ps.executeQuery();
			
			while (rs.next()) {
			s_typeid = rs.getString("typeid");
			html.append("<option value=\"");
			html.append(s_typeid);
			html.append("\" ");
			if (selected != null && selected.length() > 0
					&& s_typeid.equals(selected))
			html.append("selected");
			html.append(">");
			html.append(rs.getString("typename"));
			html.append("</option>");
			}
			rs.close();
			rs = null;
			ps.close();
			ps=null;
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
		
		html.append("</select>");
		try{
			pageContext.getOut().print(html.toString());
		}catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html=null;
		return this.SKIP_BODY;
	}

	public void setName(String newName) {
		name = newName;
	}

	public void setId(String newId) {
		id = newId;
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public void setFirstvalue(String firstvalue) {
		this.firstvalue = firstvalue;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}


	


}
